import api from '@/store/api';
import i18n from '@/i18n';
const { t } = i18n.global as any;

export interface bmcInfo {
  [x: string]: any;
  DateTime: string | number | Date;
  Description: any;
  FirmwareVersion: any;
  GraphicalConsole: {
    ConnectTypesSupported: any;
    ServiceEnabled: any;
    MaxConcurrentSessions: any;
  };
  Status: { Health: any; HealthRollup: any; State: any };
  Id: any;
  LastResetTime: string | number | Date;
  LocationIndicatorActive: any;
  Location: { PartLocation: { ServiceLabel: any } };
  Manufacturer: any;
  ManagerType: any;
  Model: any;
  Name: any;
  PartNumber: any;
  PowerState: any;
  SerialConsole: {
    ConnectTypesSupported: any;
    ServiceEnabled: any;
    MaxConcurrentSessions: any;
  };
  SerialNumber: any;
  ServiceEntryPointUUID: any;
  SparePartNumber: any;
  UUID: any;
}

export const useBmcStore = defineStore('BmcStore', () => {
  const bmc = ref({} as bmcInfo);
  const setBmcInfo = (data: bmcInfo) => {
    bmc.value.dateTime = new Date(data.DateTime);
    bmc.value.description = data.Description;
    bmc.value.firmwareVersion = data.FirmwareVersion;
    bmc.value.graphicalConsoleConnectTypes =
      data.GraphicalConsole.ConnectTypesSupported;
    bmc.value.graphicalConsoleEnabled = data.GraphicalConsole.ServiceEnabled;
    bmc.value.graphicalConsoleMaxSessions =
      data.GraphicalConsole.MaxConcurrentSessions;
    bmc.value.health = data.Status.Health;
    bmc.value.healthRollup = data.Status.HealthRollup;
    bmc.value.id = data.Id;
    bmc.value.lastResetTime = new Date(data.LastResetTime);
    bmc.value.identifyLed = data.LocationIndicatorActive;
    bmc.value.locationNumber = data.Location?.PartLocation?.ServiceLabel;
    bmc.value.manufacturer = data.Manufacturer;
    bmc.value.managerType = data.ManagerType;
    bmc.value.model = data.Model;
    bmc.value.name = data.Name;
    bmc.value.partNumber = data.PartNumber;
    bmc.value.powerState = data.PowerState;
    bmc.value.serialConsoleConnectTypes =
      data.SerialConsole.ConnectTypesSupported;
    bmc.value.serialConsoleEnabled = data.SerialConsole.ServiceEnabled;
    bmc.value.serialConsoleMaxSessions =
      data.SerialConsole.MaxConcurrentSessions;
    bmc.value.serialNumber = data.SerialNumber;
    bmc.value.serviceEntryPointUuid = data.ServiceEntryPointUUID;
    bmc.value.sparePartNumber = data.SparePartNumber;
    bmc.value.statusState = data.Status.State;
    bmc.value.uuid = data.UUID;
    bmc.value.uri = data['@odata.id'];
  };
  const getBmcInfo = async () => {
    return await api
      .get('/redfish/v1/Managers/bmc')
      .then(({ data }) => setBmcInfo(data))
      .catch((error) => console.log(error));
  };
  const updateIdentifyLedValue = async (led: {
    uri: string;
    identifyLed: any;
  }) => {
    const uri = led.uri;
    const updatedIdentifyLedValue = {
      LocationIndicatorActive: led.identifyLed,
    };
    return await api
      .patch(uri, updatedIdentifyLedValue)
      .then(() => getBmcInfo())
      .catch((error) => {
        getBmcInfo();
        console.log('error', error);
        if (led.identifyLed) {
          throw new Error(
            t('pageInventory.toast.errorEnableIdentifyLed') as string,
          );
        } else {
          throw new Error(
            t('pageInventory.toast.errorDisableIdentifyLed') as string,
          );
        }
      });
  };

  return { bmc, setBmcInfo, getBmcInfo, updateIdentifyLedValue };
});
